package easycloud.ecs.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class ProviderConfigUtils {

	private static final Log logger = LogFactory
			.getLog(ProviderConfigUtils.class);

	/**
	 * 
	 * @return
	 * @throws ConfigException
	 */
	public static ProviderConfig getProviderConfig(File configFile)
			throws ConfigException {
		ProviderConfig config = new ProviderConfig();
		Properties prop = new Properties();
		try {
			FileInputStream fis = new FileInputStream(configFile);
			prop.load(fis);
			config.setAccessKey(prop.getProperty("accessKey"));
			config.setSecretKey(prop.getProperty("secretKey"));
			config.setBucketName(prop.getProperty("bucketName"));
			config.setClassName(prop.getProperty("className"));
			config.setProviderName(prop.getProperty("providerName"));
			config.setConfigFile(configFile);
			fis.close();
		} catch (FileNotFoundException e) {
			ProviderConfigUtils.logger.warn("Config file not found", e);
			throw new ConfigException("Config file not found: " + configFile);
		} catch (IOException e) {
			ProviderConfigUtils.logger.warn("IO Exception", e);
			throw new ConfigException("IO Exception");
		}
		if (ProviderConfigUtils.isValid(config)) {
			return config;
		} else {
			return null;
		}
	}

	public static List<ProviderConfig> getProviderConfigs(File[] configFiles) {
		List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>(
				configFiles.length);
		for (File configFile : configFiles) {
			ProviderConfig providerConfig;
			try {
				providerConfig = ProviderConfigUtils
						.getProviderConfig(configFile);
				if (providerConfig != null) {
					providerConfigs.add(providerConfig);
				}
			} catch (ConfigException e) {
				ProviderConfigUtils.logger
						.warn("Get provider config failed", e);
				continue;
			}
		}
		return providerConfigs;
	}

	private static boolean isValid(ProviderConfig config) {
		boolean result = false;
		try {
			Validate.notEmpty(config.getAccessKey(), "Access Key Empty");
			Validate.notEmpty(config.getSecretKey(), "Secret Key Empty");
			Validate.notEmpty(config.getClassName(), "Class Name Empty");
			Validate.notEmpty(config.getProviderName(), "Provider Name Empty");
			result = true;
		} catch (IllegalArgumentException e) {
			ProviderConfigUtils.logger.warn("Valid config failed: "
					+ e.getMessage());
		}
		return result;
	}

	/**
	 * 
	 * @param config
	 * @throws ConfigException
	 */
	public static void saveProviderConifg(ProviderConfig config)
			throws ConfigException {
		Properties prop = new Properties();
		try {
			if (!ProviderConfigUtils.isValid(config)) {
				throw new ConfigException("Provider Config is not valid");
			}
			prop.setProperty("accessKey", config.getAccessKey());
			prop.setProperty("secretKey", config.getSecretKey());
			prop.setProperty("bucketName", config.getBucketName());
			prop.setProperty("className", config.getClassName());
			prop.setProperty("providerName", config.getProviderName());
			FileOutputStream fos = new FileOutputStream(config.getConfigFile());
			prop.store(fos, "");
			fos.close();
		} catch (FileNotFoundException e) {
			ProviderConfigUtils.logger.warn("Config file not found", e);
			throw new ConfigException("Config file not found");
		} catch (IOException e) {
			ProviderConfigUtils.logger.warn("IO Exception", e);
			throw new ConfigException("IO Exception");
		}
	}

	private ProviderConfigUtils() {

	}

}
